 /*      SClassInteger.cpp
 *
 *      Copyright 2009-2010 virpool <virpool@ukrpost.ua>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

#include "SClassInteger.h"
#include "SClassString.h"

SClassInteger::SClassInteger(string name): SObject(name, "Integer")
{
	GetMethods()->erase("ToString");
	GetMethods()->insert(pair<string, s_func>("ToString", &SClassInteger::ToString));
	GetMethods()->insert(pair<string, s_func>("Abs", &SClassInteger::Abs));
	GetMethods()->insert(pair<string, s_func>("Rand", &SClassInteger::Rand));
	GetMethods()->insert(pair<string, s_func>("Sqr", &SClassInteger::Sqr));
	GetMethods()->insert(pair<string, s_func>("Step", &SClassInteger::Step));
	GetMethods()->insert(pair<string, s_func>("ConvToStr", &SClassInteger::ConvToStr));
}

int SClassInteger::ToString(SObject* obj, s_param* res, param_list* args)
{
	if (obj == NULL || res == NULL)
	{
		return S_INVOKE_INCORRECT;
	}
	res->type = S_STRING;
	string *s = new string();
	(*s) = itoa(((SClassInteger*)obj)->Get(), 10);
	res->val = s;
	return S_INVOKE_OK;
}

int SClassInteger::Rand(SObject* obj, s_param* res, param_list* args)
{
	static string r = "Invoke ok.";
	cerr << "in method Rand\n";
	res->type = S_STRING;
	res->val = &r;
	((SClassInteger*)obj)->Set(rand());
	return S_INVOKE_OK;
}

int SClassInteger::Abs(SObject* obj, s_param* res, param_list* args)
{
	static string r = "Invoke ok.";
	cerr << "in method Abs\n";
	res->type = S_STRING;
	res->val = &r;
	((SClassInteger*)obj)->Set(abs(((SClassInteger*)obj)->Get()));
	return S_INVOKE_OK;
}

int SClassInteger::Sqr(SObject* obj, s_param* res, param_list* args)
{
	static string r = "Invoke ok.";
	cerr << "in method Sqr\n";
	res->type = S_STRING;
	res->val = &r;
	((SClassInteger*)obj)->Set(((SClassInteger*)obj)->Get() * ((SClassInteger*)obj)->Get());
	return S_INVOKE_OK;
}

int SClassInteger::Step(SObject* obj, s_param* res, param_list* args)
{
	static string r = "Invoke ok.";
	cerr << "in method Step\n";
	if (args->size() == 0)
		return S_INVOKE_INCORRECT;
	param_list::iterator p = args->begin();
	if (p->type != S_INT)
		return S_INVOKE_INCORRECT;
	int v = *((int*)p->val), rr = ((SClassInteger*)obj)->Get();
	cerr << "step = " << v << endl;
	res->type = S_STRING;
	res->val = &r;
	for (int i = 1; i < v; i++)
	{
		rr *= ((SClassInteger*)obj)->Get();
	}
	((SClassInteger*)obj)->Set( rr );
	return S_INVOKE_OK;
}

int SClassInteger::ConvToStr(SObject* obj, s_param* res, param_list* args)
{
	if (obj == NULL || args == NULL)
		return S_INVOKE_INCORRECT;
	param_list::iterator p = args->begin();
	if (p->type != S_OBJECT)
	{
		return S_INVOKE_INCORRECT;
	}
	if (((SClassInteger*)p->val)->GetClassName() != "String")
		return S_INVOKE_INCORRECT;
	((SClassString*)p->val)->Set(itoa(((SClassInteger*)obj)->Get(), 10));
	return S_INVOKE_OK;
}

char* SClassInteger::itoa(int val, int base)
{
	bool l = false;
	
	if (val < 0) 
	{
		val *= -1;
		l = true;
	}

	static char buf[32] = {0};
	
	for (int i = 0; i < 32; buf[i] = '\0', i++);

	if (val == 0)
	{
		buf[0] = '0';
		buf[1] = '\0';
		return &buf[0];
	}
	
	int i = 30;

	for(; val && i ; --i, val /= base)

		buf[l == true ? i + 1 : i] = "0123456789abcdef"[val % base];

	if (l)
	{
		buf[i+1] = '-';
	}
	
	return &buf[i+1];

}
